let questionInfoApp = new Vue({
    el: '#questionInfoApp',
    data: {
        tests:{
            questiontexts:'已解决',
            answertexts:'未采纳'
        },
        flag:true,
        collects:0,
        question: {
            title: 'Vue中的v-text和v-html有什么区别？',
            content: '感觉都是用来设置标签内部显示的内容的，区别在哪里呢？',
            userNickName: '天下无敌',
            createdTimeText: '58分钟前',
            hits: 998,
            tags: [
                { id: 5, name: 'Java SE' },
                { id: 7, name: 'Spring' },
                { id: 16, name: 'Mybatis' }
            ]
        }
     },
    methods: {
        loadQuestion: function () {
            let id = location.search;
            if (!id) {
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            id = id.substring(1);
            if (!id || isNaN(id)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            $.ajax({
                url: '/api/v1/questions/' + id,
                success: function(json) {
                    if (json.state == 2000) {
                        let question = json.data;
                        question.createdTimeText = getCreatedTimeText(question.createdTime);
                        questionInfoApp.question = question;
                    } else {
                        alert(json.message);
                        location.href = "/index.html";
                    }
                }
            });
        },
        insertcollect:function () {
            let id = location.search;
            if (!id) {
                alert("1111111111111非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            id = id.substring(1);
            if (!id || isNaN(id)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }

            $.ajax({
                url:'/api/v1/userCollection/post/'+id,
                success:function (json) {
                    if (json.state == 2000) {

                        let date = json.data;

                        date.userId = json.data.userId;
                        date.questionId = id;
                        date.createdTimeText = getCreatedTimeText(date.createdTime);
                        questionInfoApp.flag=false;
                        questionInfoApp.collects+=1;
                        userCollectionAPP.collections = date;
                    }else{
                        alert(json.message);
                    }
                }
            });
        },
        deleteCollect:function () {
            let questionId = location.search;
            if (!questionId) {
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            questionId = questionId.substring(1);
            if (!questionId || isNaN(questionId)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            $.ajax({
                url: '/api/v1/userCollection/delect/'+questionId,
                type: 'post',
                success: function(json) {

                    if (json.state == 2000) {

                        questionInfoApp.flag=true;
                        questionInfoApp.collects-=1;
                    } else {
                        alert(json.message);
                    }
                }
            });
        },
        selectCollect:function () {
            let selectByIds = location.search;
            if (!selectByIds) {
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            selectByIds = selectByIds.substring(1);
            if (!selectByIds || isNaN(selectByIds)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            $.ajax({
                url: '/api/v1/userCollection/selectByIds/'+selectByIds,
                type: 'post',
                success: function(json) {

                    if (json.state == 2000) {

                        questionInfoApp.flag=true;
                    } else {
                        questionInfoApp.flag=false;
                    }
                }
            });
        },
        countCollect:function () {
            let questionId = location.search;
            if (!questionId) {
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            questionId = questionId.substring(1);
            if (!questionId || isNaN(questionId)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            $.ajax({
                url: '/api/v1/userCollection/countCollect/'+questionId,
                type: 'post',
                success: function(json) {
                    questionInfoApp.collects=json.data;
                }
            });
        }
    },
    created: function () {
        this.loadQuestion();
        this.selectCollect();
        this.countCollect();

    }
});



